package cn.signalpha.modules.shop.controller.admin;

import cn.signalpha.common.constant.AppConstant;
import cn.signalpha.common.constant.MsgConstant;
import cn.signalpha.common.utils.Assert;
import cn.signalpha.core.boot.ctrl.BaseController;
import cn.signalpha.core.log.exception.ServiceException;
import cn.signalpha.core.mp.support.Condition;
import cn.signalpha.core.mp.support.Query;
import cn.signalpha.core.tool.api.R;
import cn.signalpha.core.tool.support.Kv;
import cn.signalpha.core.tool.utils.BeanUtil;
import cn.signalpha.core.tool.utils.Func;
import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.com.service.UserService;
import cn.signalpha.modules.shop.dto.PlaceLevelDTO;
import cn.signalpha.modules.shop.entity.PlaceLevel;
import cn.signalpha.modules.shop.service.PlaceLevelService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.*;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 控制器
 *
 * @author signalpha
 */
@RestController
@AllArgsConstructor
@RequestMapping(AppConstant.APPLICATION_SHOP_NAME + "/admin/place/level")
@Api(value = "分销员等级", tags = "分销员等级")
public class PlaceLevelController extends BaseController {

	private final UserService userService;
	private final PlaceLevelService placeLevelService;

    @GetMapping("/page")
    @ApiImplicitParams({
        @ApiImplicitParam(name="level", dataType = "string", value = "等级"),
        @ApiImplicitParam(name="name", dataType = "string", value = "名称"),
    })
    @ApiOperation(value = "分页", notes = "")
    public R<IPage<PlaceLevel>> page(@ApiIgnore @RequestParam Map<String, Object> params, Query query) {
        QueryWrapper<PlaceLevel> wrapper = placeLevelService.selectPlaceLevelWrapper(params);
        return R.data(placeLevelService.page(Condition.getPage(query), wrapper));
    }

    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "")
    public R<List<PlaceLevel>> list(@ApiIgnore @RequestParam Map<String, Object> params) {
        return R.data(placeLevelService.list(placeLevelService.selectPlaceLevelWrapper(params)));
    }

	@GetMapping("/info")
	@ApiOperation(value = "详情", notes = "传入id")
	public R<PlaceLevel> info(@ApiParam(value = "主键", required = true) @RequestParam Long id) {
		return R.data(placeLevelService.getById(id));
	}

	@PostMapping("/save")
	@ApiOperation(value = "新增", notes = "传入placeLevel")
	public R save(@Valid @RequestBody PlaceLevelDTO placeLevelDTO) {
		Long sellerId = Long.valueOf(getUser().getTenantId());
		if(placeLevelDTO.getLevel() > 8){
			throw new ServiceException("最大支持8个等级");
		}
		if(ObjectUtil.isNotEmpty(placeLevelService.getByLevel(placeLevelDTO.getLevel()))){
			throw new ServiceException("等级级别重复");
		}
		if(ObjectUtil.isNotEmpty(placeLevelService.getByName(placeLevelDTO.getName()))){
			throw new ServiceException("等级名称重复");
		}
		placeLevelDTO.setSellerId(sellerId);
		return R.status(placeLevelService.save(BeanUtil.copy(placeLevelDTO, PlaceLevel.class)));
	}

	@PostMapping("/update")
	@ApiOperation(value = "修改", notes = "传入placeLevel")
	public R update(@Valid @RequestBody PlaceLevelDTO placeLevelDTO) {
		Long sellerId = Long.valueOf(getUser().getTenantId());
		if(placeLevelDTO.getLevel() > 8){
			throw new ServiceException("最大支持8个等级");
		}
		PlaceLevel placeLevel = placeLevelService.getById(placeLevelDTO.getId());
		Assert.isNull(placeLevel, MsgConstant.OBJECT_NULL);
		if(!Objects.equals(placeLevelDTO.getLevel(), placeLevel.getLevel())){
			if(ObjectUtil.isNotEmpty(placeLevelService.getByLevel(placeLevelDTO.getLevel()))){
				throw new ServiceException("等级级别重复");
			}
		}
		if(!Objects.equals(placeLevelDTO.getName(), placeLevel.getName())){
			if(ObjectUtil.isNotEmpty(placeLevelService.getByName(placeLevelDTO.getName()))){
				throw new ServiceException("等级名称重复");
			}
		}
		placeLevelDTO.setSellerId(sellerId);
		return R.status(placeLevelService.updateById(BeanUtil.copy(placeLevelDTO, PlaceLevel.class)));
	}

	@PostMapping("/remove")
	@ApiOperation(value = "删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		Func.toLongList(ids).forEach(id -> {
			PlaceLevel placeLevel = placeLevelService.getById(id);
			if(userService.countByParams(Kv.create().set("placeLevel", placeLevel.getLevel())) > 0){
				throw new ServiceException("该等级已关联用户，请先解除绑定关系");
			}
		});
		return R.status(placeLevelService.removeByIds(Func.toLongList(ids)));
	}

}
